package com.abc.cloud.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static com.abc.cloud.common.constants.Constants.CAPTCHA_CODE;

@Api(tags = "验证码接口")
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 线段干扰验证码
     * @param httpServletResponse
     */
    @ApiOperation(value = "线段干扰验证码")
    @GetMapping("/line")
    public void lineCaptcha(HttpServletResponse httpServletResponse,
                            @RequestParam("key") String key){

        ServletOutputStream out;
        try {
            out = httpServletResponse.getOutputStream();
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
            saveCode(lineCaptcha.getCode(), key);
            lineCaptcha.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 圆圈干扰验证码
     * @param httpServletResponse
     */
    @ApiOperation(value = "圆圈干扰验证码")
    @GetMapping("/circle")
    public void circleCaptcha(HttpServletResponse httpServletResponse,
                              @RequestParam("key") String key){

        ServletOutputStream out;
        try {
            out = httpServletResponse.getOutputStream();
            CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
            saveCode(circleCaptcha.getCode(), key);
            circleCaptcha.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 扭曲干扰验证码
     * @param httpServletResponse
     */
    @ApiOperation(value = "扭曲干扰验证码")
    @GetMapping("/shear")
    public void shearCaptcha(HttpServletResponse httpServletResponse,
                             @RequestParam("key") String key){

        ServletOutputStream out;
        try {
            out = httpServletResponse.getOutputStream();
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
            saveCode(lineCaptcha.getCode(), key);
            lineCaptcha.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存到redis
     * @param code
     */
    private void saveCode(String code, String key) {
        redisTemplate.opsForValue().set(CAPTCHA_CODE + key, code, 3, TimeUnit.MINUTES);
    }
}
